﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AttrDict = System.Collections.Generic.Dictionary<string, string>;

namespace ConciseMarkupLanguage.Utils
{
    public static class Html
    {
        public static string EscapeQuotes(this string str)
        {
            return str.Replace("\"", "&quot;");
        }

        public static string Tag(string tagName, AttrDict attrs=null, string content = null)
        {
            var sb = new StringBuilder("<");
            sb.Append(tagName);
            if (attrs != null)
            {
                foreach (var attr in attrs)
                {
                    if (!false.Equals(attr.Value))
                    {
                        sb.AppendFormat(" {0}", attr.Key);
                        if (attr.Value != null) sb.AppendFormat("=\"{0}\"", true.Equals(attr.Value) ? attr.Key.EscapeQuotes() : attr.Value.ToString().EscapeQuotes());
                    }
                }
            }
            if (content != null) sb.AppendFormat(">{0}</{1}>", content, tagName);
            else sb.Append(" />");
            return sb.ToString();
        }
    }
}
